import {
	getCurrentInstance
} from "vue";
const instance = getCurrentInstance();
const styleObj = {
	success: {
		icon: '/static/image/icon/success.png',
		color: '#00A870',
	},
	warning: {
		icon: '/static/image/icon/warning.png',
		color: '#ED7B2F',
	},
	info: {
		icon: '/static/image/icon/info.png',
		color: '#0052D9',
	},
	error: {
		icon: '/static/image/icon/error.png',
		color: '#E34D59',
	},
}

/**
 *
 * @param {String} text
 * @param {String} time // 毫秒
 */
export const messageClass = (text, time = 3000) => {
	const view = handleMessage('info', text, time)
	return view
}

if (!messageClass.success) {
	eachMessage()
}


// 仅app平台的代码
// #ifdef APP-PLUS
// plus
const bg = new plus.nativeObj.Bitmap('bg')
bg.load('/static/image/icon/bg.png')

const bitmapSuccess = new plus.nativeObj.Bitmap('bmp1')
const bitmapWarning = new plus.nativeObj.Bitmap('bmp2')
const bitmapInfo = new plus.nativeObj.Bitmap('bmp3')
const bitmapError = new plus.nativeObj.Bitmap('bmp4')

const icon = {
	success: new plus.nativeObj.Bitmap('bmp1'),
	warning: new plus.nativeObj.Bitmap('bmp2'),
	info: new plus.nativeObj.Bitmap('bmp3'),
	error: new plus.nativeObj.Bitmap('bmp4'),
}
icon.success.load(styleObj.success.icon)
icon.warning.load(styleObj.warning.icon)
icon.info.load(styleObj.info.icon)
icon.error.load(styleObj.error.icon)

const handleMessage = (type, text, time) => {
	// 创建控件
	const view = new plus.nativeObj.View('test', {
		top: '20%',
		left: '10%',
		height: '8%',
		width: '80%',
		backgroundColor: '#00000000'
	})
	view.drawBitmap(bg, {
		top: '0px',
		left: '0px',
		width: '100%',
		height: '100%'
	}, {
		top: 'auto',
		left: 'auto',
		width: '100%',
		height: '100%'
	});
	view.drawBitmap(icon[type], {
		top: '0px',
		left: '0px',
		width: '100%',
		height: '100%'
	}, {
		top: 'auto',
		left: '5%',
		width: '25px',
		height: '25px'
	});
	view.drawText(text, {
		top: '2px',
		left: '16%',
		width: '80%',
		height: '90%'
	}, {
		color: styleObj[type].color,
		whiteSpace: 'normal',
		align: 'left',
		verticalAlign: 'middle',
		overflow: 'ellipsis'
	});
	view.show();
	if (time) {
		setTimeout(() => {
			view.close();
		}, time)
	}
	return view
}
// #endif

export default function(title = '数据加载失败！', icon = 'none', duration = 1500) {
	// #ifdef APP-PLUS
	const handleMessage = (type, text, time) => {
		let port = uni.getSystemInfoSync().platform
		if (port === 'IOS') {
			plus.nativeUI.toast(text);
		} else {
			uni.showToast({
				title: text,
				duration,
				icon
			})
		}
	}
	// #endif

	// #ifdef APP-PLUS
	handleMessage(icon, title, duration)
	// #endif

	// #ifdef H5
	if (icon === 'warning') {
		icon = 'error'
	}
	uni.showToast({
		title: title,
		duration: duration,
		icon: icon
	})
	// #endif
	console.log(icon)
}


function eachMessage() {
	;
	['success', 'warning', 'info', 'error'].forEach((type) => {
		messageClass[type] = (text, time = 3000) => {
			const view = handleMessage(type, text, time)
			return view
		}
	})
}